package com.whfc.ms.api.base.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppBoardEmpOverviewDTO;
import com.whfc.emp.dto.EmpDataDTO;
import com.whfc.emp.dto.train.AppBoardEmpGroupCountDTO;
import com.whfc.entity.dto.board.BoardProjectDataDTO;
import com.whfc.fuum.dto.AppProjectDTO;
import com.whfc.fuum.dto.AppProjectDetailDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.AppBoardMachOverviewDTO;
import com.whfc.ms.api.base.service.AppBoardService;
import com.whfc.ms.api.base.service.MsAppProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 大屏展示
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年9月10日
 */
@RequestMapping("/ms/api/board")
@RestController
public class AppBoardController extends BaseController {

    @Autowired
    private AppBoardService appBoardService;

    @Autowired
    private MsAppProjectService appProjectService;

    /**
     * 获取项目切换的项目列表
     *
     * @param user
     * @return
     */
    @Deprecated
    @GetMapping("/projectList")
    public Result projectList(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        logger.info("获取项目切换的项目列表,loginer:{}", user.getUsername());
        ListData<AppProjectDTO> pageData = appProjectService.list(user);
        return ResultUtil.success(pageData);
    }

    /**
     * 大屏展示-项目概况
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/project/overview")
    public Result projectOverview(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("deptId") Integer deptId) {
        logger.info("大屏展示-项目概况，loginer:{},deptId:{}", user.getUsername(), deptId);
        AppProjectDetailDTO appProjectDetailDTO = appBoardService.projectOverview(deptId);
        return ResultUtil.success(appProjectDetailDTO);
    }

    @GetMapping("/project/data/latest")
    public Result getProjectLatestData(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("deptId") Integer deptId) {
        logger.info("大屏展示-项目数据，loginer:{},deptId:{}", user.getUsername(), deptId);
        BoardProjectDataDTO data = appBoardService.getProjectLatestData(deptId);
        return ResultUtil.success(data);
    }

    /**
     * 大屏展示-查询设备运行统计数据
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/mach/runData")
    public Result machRunData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("deptId") Integer deptId,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate) {
        logger.info("大屏展示-查询设备运行统计数据，loginer:{},deptId:{}", user.getUsername(), deptId);
        AppBoardMachOverviewDTO appBoardMachOverviewDTO = appBoardService.machRunData(deptId, startDate, endDate);
        return ResultUtil.success(appBoardMachOverviewDTO);
    }


    /**
     * 大屏展示-查询设备列表及统计数据
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/mach/list")
    public Result machList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("deptId") Integer deptId) {
        logger.info("大屏展示-查询设备列表及统计数据，loginer:{},deptId:{}", user.getUsername(), deptId);
        AppBoardMachOverviewDTO appBoardMachOverviewDTO = appBoardService.machList(deptId);
        return ResultUtil.success(appBoardMachOverviewDTO);
    }


    @GetMapping("/emp/getData")
    public Result getEmpData(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("deptId") Integer deptId) {
        logger.info("大屏-人员，loginer:{},deptId:{}", user.getUsername(), deptId);
        AppBoardEmpOverviewDTO empData = appBoardService.getEmpData(deptId);
        return ResultUtil.success(empData);
    }

    @GetMapping("/emp/group/data")
    public Result getEmpGroupData(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("deptId") Integer deptId) {
        logger.info("大屏-人员班组统计，loginer:{},deptId:{}", user.getUsername(), deptId);
        List<AppBoardEmpGroupCountDTO> list = appBoardService.getEmpGroupData(deptId);
        return ResultUtil.success(new ListData<>(list));
    }


    @GetMapping("/emp/attendData")
    public Result getAttendData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("deptId") Integer deptId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate) {
        logger.info("获取每天的出勤数据统计,loginer:{},deptId:{},startDate:{},endDate:{}", user.getUsername(),
                deptId, startDate, endDate);
        ListData<EmpDataDTO> listData = appBoardService.getAttendData(deptId, startDate, endDate);
        return ResultUtil.success(listData);
    }

}
